package com.hushao.lims.weblims.utils;
import com.hushao.lims.weblims.domain.TeacherInfo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import java.util.Collection;

/**
 * @author 930588706
 * @date 18/5/10
 */
@Component
public class UserUtil {
    private static   final String admin="ROLE_管理员",
            administrator="ROLE_超级管理员",
            charge="ROLE_主管",
            labAdmin="ROLE_实验室管理员";


    /**
     * 获取登录者的信息
     * @param authentication
     * @return
     * @throws Exception
     */
    public TeacherInfo isLogged(Authentication authentication)throws Exception {
        if(authentication!=null
                && authentication.isAuthenticated()
                &&!authentication.getPrincipal().toString().equals("anonymousUser")){
            return (TeacherInfo) authentication.getPrincipal();
        }else {throw new Exception("请重新登陆！");}
    }

    /**
     * 是否是管理员
     * @param teacher
     * @return
     */
    public boolean isAdmin(TeacherInfo teacher){
        Collection<? extends GrantedAuthority> authentications=teacher.getAuthorities();
        return authentications.stream().anyMatch(a->a.getAuthority().equals(admin));
    }

    /**
     * 是否是超级管理员
     * @param teacher
     * @return
     */
    public boolean isAdministrator(TeacherInfo teacher){
        Collection<? extends GrantedAuthority> authentications=teacher.getAuthorities();
        return authentications.stream().anyMatch(a->a.getAuthority().equals(administrator));
    }

    /**
     * 是否是主管
     * @param teacher
     * @return
     */
    public boolean isCharge(TeacherInfo teacher){
        Collection<? extends GrantedAuthority> authentications=teacher.getAuthorities();
        return authentications.stream().anyMatch(a->a.getAuthority().equals(charge));
    }

    /**
     * 是否是实验室管理员
     * @param teacher
     * @return
     */
    public boolean isLabAdmin(TeacherInfo teacher){
        Collection<? extends GrantedAuthority> authentications=teacher.getAuthorities();
        return authentications.stream().anyMatch(a->a.getAuthority().equals(labAdmin));
    }

}
